"""
-*- coding: utf-8 -*-
@Author : yyc
@Time : 2024/1/31 17:56
@File : documents.py
"""
# es 搜索配置文件
# python manage.py search_index --rebuild
from django_elasticsearch_dsl import Document, Index, fields
from knowledge1.models import Detail
from configs import database_paras

detail_index = Index(database_paras["elasticsearch"]["db_name"])

# 索引配置
detail_index.settings(
    number_of_shards=1,
    number_of_replicas=0
)


@detail_index.document
class DetailDocument(Document):
    id = fields.IntegerField()
    uid = fields.KeywordField()
    text = fields.TextField(
        fields={
            "keyword": {
                "type": 'text',
                "analyzer": "ik_smart",
                "search_analyzer": "ik_smart"
            }
        }
)
    detail_text = fields.TextField(
        fields={
            "keyword": {
                "type": 'text',
                "analyzer": "ik_smart",
                "search_analyzer": "ik_smart"
            }
        }

    )
    # embedding = fields.KeywordField()
    base = fields.ObjectField(properties={
        'name': fields.TextField()
    })
    status = fields.IntegerField()
    source = fields.ObjectField(properties={
        'name': fields.TextField()
    })
    # parent_id = fields.KeywordField()
    create_time = fields.DateField()

    class Django:
        model = Detail
        # 要在Elasticsearch中建立索引的模型的字段
        #  fields 置空 则会根据上方的对象的属性进行映射,  可直接写orm模型类字段名, 会根据orm中的字段类型进行自动选择文档字段类型
        fields = []
        # 执行迁移时的 每次从mysql中数据读取的条数.
        queryset_pagination = 50000

        # fields = ['uid', 'base', 'text', 'detail_text', 'embedding', 'source', 'parent_id']
